import {
  Component, OnInit, Input, ViewEncapsulation, ViewChild, ChangeDetectionStrategy,
  OnDestroy
} from '@angular/core';
import {Subject, BehaviorSubject} from "rxjs";
import {ActivatedRoute, Router} from "@angular/router";

import {ToolsService} from "@shared/service/tools/tools.service";
import {MineService} from "@mine/mine.service";
import {AuthService} from "@shared/service/auth/auth.service";

import {SeedInfiniteScroll} from "@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {ArticleVO} from "@interface/index";


@Component({
  selector: 'info',
  templateUrl: 'info.component.html',
  styleUrls: ['info.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class InfoComponent implements OnInit, OnDestroy {
  @ViewChild(SeedInfiniteScroll) infiniteScroll;
  public info: Array<ArticleVO> = [];
  public ngUnsubscribe: Subject<void> = new Subject<void>();
  public stateStore = {
    haveData: true
  };
  public params = {
    userId: '',
    publishStatus: 1,
    page: 1,
    rows: 10
  };

  constructor(public tools: ToolsService,
              public mineSer: MineService,
              public authSer: AuthService,
              private router: Router) {
    this.params.userId = this.authSer.getUserId();
  }

  getInfo() {
    this.tools.showLoading();
    this.mineSer.getMyInfo(this.params)
      .takeUntil(this.ngUnsubscribe).subscribe(res => {
      this.tools.hideLoading();
      if (res.result === '0') {
        this.params.page++;
        this.infiniteScroll && this.infiniteScroll.complete();
        const info = res.data.list;
        if (!res.data.hasNextPage) {
          if (info.length === 0) {
            this.stateStore.haveData = false;
          }
          this.infiniteScroll && this.infiniteScroll.enable(false);
        }
        this.info = [...this.info, ...info];
      }
    });
  }

  goToTarget(article) {
    if (article['infoType'] === 2) {
      this.router.navigate(['news/photo-news', article['cateId'], article['articleId']]);
    } else {
      this.router.navigate(['/news', article['articleId']]);
    }
  }

  ngOnInit() {
    this.getInfo();
  }

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }
}
